home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1999 #2 / Amiga Plus CD - 1999 - No. 2.iso / System-Boost / Workbench / ToolManager / Source / Library / screen.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  5KB  |  195 lines

  1. /*
  2.  * screen.c  V3.1
  3.  *
  4.  * ToolManager ScreenNotify handling routines
  5.  *
  6.  * Copyright (C) 1990-98 Stefan Becker
  7.  *
  8.  * This source code is for educational purposes only. You may study it
  9.  * and copy ideas or algorithms from it for your own projects. It is
  10.  * not allowed to use any of the source codes (in full or in parts)
  11.  * in other programs. Especially it is not allowed to create variants
  12.  * of ToolManager or ToolManager-like programs from this source code.
  13.  *
  14.  */
  15.  
  16. #include "toolmanager.h"
  17.  
  18. /* Local data */
  19. static const char      WorkbenchName[]       = "Workbench";
  20. static struct MsgPort *ScreenNotifyPort      = NULL;
  21. static ULONG           ScreenNotifyLockCount = 0;
  22. static struct Library *ScreenNotifyBase      = NULL;
  23. static APTR            CloseScreenHandle;
  24. static APTR            PubScreenHandle;
  25. static APTR            WorkbenchHandle;
  26.  
  27. /* Start ScreenNotify */
  28. #define DEBUGFUNCTION StartScreenNotify
  29. LONG StartScreenNotify(void)
  30. {
  31.  LONG rc = -1;
  32.  
  33.  SCREEN_LOG(LOG0(Entry))
  34.  
  35.  /* Allocate message port */
  36.  if (ScreenNotifyPort = CreateMsgPort()) rc = ScreenNotifyPort->mp_SigBit;
  37.  
  38.  SCREEN_LOG(LOG2(Result, "Port 0x%08lx Signal %ld", ScreenNotifyPort, rc))
  39.  
  40.  return(rc);
  41. }
  42.  
  43. /* Stop ScreenNotify */
  44. #undef  DEBUGFUNCTION
  45. #define DEBUGFUNCTION StopScreenNotify
  46. void StopScreenNotify(void)
  47. {
  48.  SCREEN_LOG(LOG1(Port, "0x%08lx", ScreenNotifyPort))
  49.  
  50.  DeleteMsgPort(ScreenNotifyPort);
  51. }
  52.  
  53. /* Lock ScreenNotify */
  54. #undef  DEBUGFUNCTION
  55. #define DEBUGFUNCTION LockScreenNotify
  56. void LockScreenNotify(void)
  57. {
  58.  /* ScreenNotify already opened? */
  59.  if (ScreenNotifyBase != NULL)
  60.  
  61.   /* Yes, increment lock count */
  62.   ScreenNotifyLockCount++;
  63.  
  64.  /* No, open it */
  65.  else if (ScreenNotifyBase = OpenLibrary("screennotify.library", 0)) {
  66.  
  67.   /* Add clients */
  68.   CloseScreenHandle = AddCloseScreenClient(NULL, ScreenNotifyPort, 0);
  69.   PubScreenHandle   = AddPubScreenClient(ScreenNotifyPort, 0);
  70.   WorkbenchHandle   = AddWorkbenchClient(ScreenNotifyPort, 0);
  71.  
  72.   /* Set lock count to 1 */
  73.   ScreenNotifyLockCount = 1;
  74.  }
  75. }
  76.  
  77. /* Release ScreenNotify */
  78. #undef  DEBUGFUNCTION
  79. #define DEBUGFUNCTION ReleaseScreenNotify
  80. void ReleaseScreenNotify(void)
  81. {
  82.  /* ScreenNotify already opened and lock count reaches zero? */
  83.  if ((ScreenNotifyBase != NULL) && (--ScreenNotifyLockCount == 0)) {
  84.  
  85.   /* Remove clients */
  86.   if (CloseScreenHandle)
  87.    while (RemCloseScreenClient(CloseScreenHandle) == FALSE) Delay(10);
  88.   if (PubScreenHandle)
  89.    while (RemPubScreenClient(PubScreenHandle) == FALSE)     Delay(10);
  90.   if (WorkbenchHandle)
  91.    while (RemWorkbenchClient(WorkbenchHandle) == FALSE)     Delay(10);
  92.  
  93.   /* Close library */
  94.   CloseLibrary(ScreenNotifyBase);
  95.  
  96.   /* Reset library base pointer */
  97.   ScreenNotifyBase = NULL;
  98.  }
  99. }
  100.  
  101. /* Forward screen event to dock objects */
  102. #undef  DEBUGFUNCTION
  103. #define DEBUGFUNCTION ScreenEvent
  104. static void ScreenEvent(ULONG type, ULONG MethodID, void *data)
  105. {
  106.  struct MinNode *n = GetHead(GetHandleList());
  107.  
  108.  SCREEN_LOG(LOG3(Arguments, "Type %ld Method 0x%08lx 0x%08lx",
  109.                  type, MethodID, data))
  110.  
  111.  /* Traverse handle list */
  112.  while (n) {
  113.   Object *obj1 = (Object *) TMHANDLE(n)->tmh_ObjectLists[type].mlh_Head;
  114.   Object *obj2;
  115.  
  116.   SCREEN_LOG(LOG1(Handle, "0x%08lx", n))
  117.  
  118.   /* Scan 'type' object list */
  119.   while (obj2 = NextObject(&obj1)) {
  120.  
  121.    SCREEN_LOG(LOG1(Object, "0x%08lx", obj2))
  122.  
  123.    /* Send screen open/close method to object */
  124.    DoMethod(obj2, MethodID, data);
  125.   }
  126.  
  127.   /* Get next entry in handle list */
  128.   n = GetSucc(n);
  129.  }
  130. }
  131.  
  132. /* Handle ScreenNotify event */
  133. #undef  DEBUGFUNCTION
  134. #define DEBUGFUNCTION HandleScreenNotify
  135. void HandleScreenNotify(void)
  136. {
  137.  struct ScreenNotifyMessage *snm;
  138.  
  139.  SCREEN_LOG(LOG0(Entry))
  140.  
  141.  /* Retrieve message from port */
  142.  while (snm = (struct ScreenNotifyMessage *) GetMsg(ScreenNotifyPort)) {
  143.  
  144.   /* Which event? */
  145.   switch (snm->snm_Type) {
  146.    case SCREENNOTIFY_TYPE_CLOSESCREEN:
  147.     /* Close docks first */
  148.     ScreenEvent(TMOBJTYPE_DOCK, TMM_ScreenClose, snm->snm_Value);
  149.  
  150.     /* Then remove entries from image cache */
  151.     ScreenEvent(TMOBJTYPE_IMAGE, TMM_ScreenClose, snm->snm_Value);
  152.     break;
  153.  
  154.    case SCREENNOTIFY_TYPE_PUBLICSCREEN:
  155.     ScreenEvent(TMOBJTYPE_DOCK, TMM_ScreenOpen,
  156.                 ((struct PubScreenNode *) snm->snm_Value)->psn_Node.ln_Name);
  157.     break;
  158.  
  159.    case SCREENNOTIFY_TYPE_PRIVATESCREEN:
  160.     ScreenEvent(TMOBJTYPE_DOCK, TMM_ScreenClose,
  161.                 ((struct PubScreenNode *) snm->snm_Value)->psn_Screen);
  162.     break;
  163.  
  164.    case SCREENNOTIFY_TYPE_WORKBENCH:
  165.     /* Close or open event? */
  166.     switch (snm->snm_Value) {
  167.      case FALSE: {            /* Close event */
  168.        struct Screen *s;
  169.  
  170.        /* Lock Workbench screen */
  171.        if (s = LockPubScreen(WorkbenchName)) {
  172.         /* Close docks */
  173.         ScreenEvent(TMOBJTYPE_DOCK, TMM_ScreenClose, s);
  174.  
  175.         /* Remove entries from image cache */
  176.         ScreenEvent(TMOBJTYPE_IMAGE, TMM_ScreenClose, s);
  177.  
  178.         /* Unlock Workbench screen */
  179.         UnlockPubScreen(NULL, s);
  180.        }
  181.       }
  182.       break;
  183.  
  184.      case TRUE:               /* Open event */
  185.       ScreenEvent(TMOBJTYPE_DOCK, TMM_ScreenOpen, WorkbenchName);
  186.       break;
  187.     }
  188.     break;
  189.   }
  190.  
  191.   /* Reply message */
  192.   ReplyMsg((struct Message *) snm);
  193.  }
  194. }
  195.